package client.view.keys;

import java.io.IOException;
import java.util.ArrayList;
import java.util.TimerTask;

import client.main.Writer;
import client.utility.PacketId.PacketID;
import client.view.keys.Keys.Key;

public class KeyTimer extends TimerTask{
	
	private ArrayList<Integer> keys;
	private Writer writer;
	private String playerName;
	private Keys keyDB;
	
	public KeyTimer(){
		keys=new ArrayList<Integer>();
		keyDB=new Keys();
	}
	
	public void add(int kI){
		Key k=keyDB.getKey(kI);
		
		if(k!=Key.none && !keys.contains(kI))
			keys.add(kI);
	}
	
	public void remove(int k){
		keys.remove((Integer)k);
	}
	
	public void setWriter(Writer writer) {
		this.writer = writer;
	}

	public void setPlayerName(String playerName) {
		this.playerName = playerName;
	}

	public void run() {
		if(writer==null || playerName==null)
			return;
		
		for(int i=0; i<keys.size(); i++){
			int k=keys.get(i);
			try {writer.write(PacketID.userAction,keyDB.getKey(k)+";"+playerName);}
			catch (IOException e) {e.printStackTrace();}
		}
		
	}

}
